package test.src.java8;

public class PrintApple {
    public static void main(String[] args) {
        Apple redApple = new Apple("red", 30);
        Apple greenApple = new Apple("green", 20);
        Apple whiteApple = new Apple("white", 35);
        Apple[] apples = new Apple[]{redApple, greenApple, whiteApple};
        prettyPrintApple(apples, new ColorPrintFormat());
        prettyPrintApple(apples, (Apple apple) -> "A " + apple.getColor() + " apple of " + apple.getWeight() + " g.");
        prettyPrintApple(apples, (Apple apple) -> "A " + apple.getColor() + " apple.");
    }

    public static void prettyPrintApple (Apple[] apples, ApplePrintFormat applePrintFormat) {
        String output;
        for (Apple apple : apples) {
            output = applePrintFormat.accept(apple);
            System.out.println(output);
        }
    }
}

interface ApplePrintFormat {
    String accept(Apple apple);
}

class ColorPrintFormat implements ApplePrintFormat {
    @Override
    public String accept(Apple apple) {
        String output;
        output = "A " + apple.getColor() + " apple.";
        return output;
    }
}

class Apple {
    String color;
    int weight;

    Apple (String color, int weight) {
        this.color = color;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public int getWeight() {
        return weight;
    }
}